package mf;

import org.apache.commons.codec.digest.DigestUtils;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * Created by Adiljan on 2016-10-30.
 */
public class MagicCube {
	/**
	 * 完成样式MD5
	 */
	private final String complete;
	Face green = new Face(Color.green, "Green");
	Face magenta = new Face(Color.magenta, "Magenta");
	Face blue = new Face(Color.blue, "Blue");
	Face red = new Face(Color.red, "Red");
	Face yellow = new Face(Color.yellow, "Yellow");
	Face white = new Face(Color.white, "White");
	Face[] faces = new Face[]{green, magenta, blue, red, yellow, white};

	public MagicCube() {
		complete = getHex();
	}

	public String getData() {
		return green.toString() + magenta + blue + red + yellow + white;
	}

	public String getHex() {
		return DigestUtils.md5Hex(getData());
	}

	public boolean isComplete() {
		return complete.equals(getHex());
	}


	public BufferedImage toImage() {
		return toImage(50);
	}

	private BufferedImage toImage(int size) {
		int gap = 5;
		int total = faces.length;
		int faceSpaceX = (total + 1) * 15;
		int blockSpaceX = gap * 12;
		int spaceY = gap * 4;
		int width = size * total * 3 + faceSpaceX + blockSpaceX;
		int height = size * 3 + spaceY;
		BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bufferedImage.createGraphics();
//		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);


		for (int i = 0; i < faces.length; i++) {
			Face face = faces[i];
			Block[] blocks = face.getData();

			for (int j = 0, h = 0; j < blocks.length; j++, h = size * j / 3 + gap * j / 3) {
				Block block = blocks[i];
				g.setColor(block.getColor());
				g.fillRect(size * j + gap * j, h, size, size);
			}
		}
		g.dispose();
		return bufferedImage;
	}

}
